package cn.vihub.service.impl;

import cn.hutool.core.lang.tree.TreeUtil;
import cn.vihub.handler.MyRuntimeException;
import cn.vihub.pojo.ShopRoleToMenu;
import cn.vihub.pojo.ShopUserToRole;
import cn.vihub.service.ShopRoleToMenuService;
import cn.vihub.service.ShopUserToRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.vihub.pojo.ShopMenu;
import cn.vihub.service.ShopMenuService;
import cn.vihub.mapper.ShopMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 64452
* @description 针对表【shop_menu】的数据库操作Service实现
* @createDate 2025-07-17 20:43:24
*/
@Service
public class ShopMenuServiceImpl extends ServiceImpl<ShopMenuMapper, ShopMenu>
    implements ShopMenuService{

    @Autowired
    private ShopUserToRoleService shopUserToRoleService;

    @Autowired
    private ShopRoleToMenuService shopRoleToMenuService;

    @Override
    public Object getTree() {
        // 查询所有
        List<ShopMenu> list = list();

        // 循环递归菜单树
        return TreeUtil.build(list, "0", (shopMenu, tree) -> {
           tree.setId(shopMenu.getId()+"");
           tree.setName(shopMenu.getName());
           tree.setParentId(shopMenu.getPid()+"");
           tree.putExtra("uri", shopMenu.getUri());
           tree.putExtra("typeId", shopMenu.getTypeId());
            tree.putExtra("code", shopMenu.getCode());
            tree.putExtra("pidPath", shopMenu.getPidPath());
        });

    }

    @Override
    public void isMenuByPid(Integer id) {

        LambdaQueryWrapper<ShopMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShopMenu::getPid, id);

        if (count(lambdaQueryWrapper) > 0){
            throw new MyRuntimeException("菜单下存在子菜单，不能删除");
        }

    }

    @Override
    public Object treeByUserId(Integer userId) {

        // 查询用户所拥有的角色
        List<ShopUserToRole> shopUserToRoleList = shopUserToRoleService.listByUserId(userId);
        List<Integer> roleIdList = shopUserToRoleList.stream().map(ShopUserToRole::getRoleId).collect(Collectors.toList());

        // 查询菜单的id
        List<ShopRoleToMenu> shopRoleToMenuList = shopRoleToMenuService.listByRoleIdList(roleIdList);
        if (shopRoleToMenuList.size() == 0){
            throw new MyRuntimeException("该用户还没有关联菜单");
        }
        List<Integer> menuIdList = shopRoleToMenuList.stream().map(ShopRoleToMenu::getMenuId).collect(Collectors.toList());

        List<ShopMenu> shopMenus = listByIds(menuIdList);

        return TreeUtil.build(shopMenus, "0", (shopMenu, tree) -> {
            tree.setId(shopMenu.getId()+"");
            tree.setName(shopMenu.getName());
            tree.setParentId(shopMenu.getPid()+"");
            tree.putExtra("uri", shopMenu.getUri());
            tree.putExtra("typeId", shopMenu.getTypeId());
        });
    }

    @Override
    public List<ShopMenu> listByRoleId(Integer roleId) {

        // 获取角色绑定得菜单得id
        List<Integer> roleList = new ArrayList<>();
        roleList.add(roleId);
        List<ShopRoleToMenu> shopRoleToMenuList = shopRoleToMenuService.listByRoleIdList(roleList);
        if (shopRoleToMenuList.size() == 0){
            throw new MyRuntimeException("该用户还没有关联菜单");
        }
        List<Integer> menuIdList = shopRoleToMenuList.stream().map(ShopRoleToMenu::getMenuId).collect(Collectors.toList());

        // in查询菜单列表
        return listByIds(menuIdList);

    }
}




